﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'PRC_TB_USER_SE')
	BEGIN
		DROP  Procedure  dbo.PRC_TB_USER_SE
	END

GO

CREATE PROCEDURE [dbo].[PRC_TB_USER_SE] 
(
    @TUID Integer = null,
    @TUNAME Varchar(150) = null,
    @TULOGIN Varchar(20) = null,
    @TUPASSWORD Varchar(50) = null,
    @TUPROFILERID Integer = null,
    @TUGROUPID Integer = null 
)
AS
	SELECT 
            a.TU_ID, 
            a.TU_NAME, 
            a.TU_LOGIN, 
            a.TU_PASSWORD, 
            a.TU_EMAIL,
            a.TU_PROFILER_ID, 
            a.TU_GROUP_ID,
            b.TG_ID,
            b.TG_NAME,
            b.TG_IDENTIFICATION,
            b.TG_DESCRIPTION,
            c.TP_ID,
            c.TP_DESCRIPTION
	    FROM TB_USER a
            INNER JOIN TB_GROUPS b
                on a.TU_GROUP_ID = b.TG_ID
            INNER JOIN TB_PROFILERS c
                on a.TU_PROFILER_ID = c.TP_ID
	    where (@TUID is null or TU_ID = @TUID)
	    and (@TUNAME is null or TU_NAME = @TUNAME)
	    and (@TULOGIN is null or TU_LOGIN = @TULOGIN)
	    and (@TUPASSWORD is null or TU_PASSWORD = @TUPASSWORD)
	    and (@TUPROFILERID is null or TU_PROFILER_ID = @TUPROFILERID)
	    and (@TUGROUPID is null or TU_GROUP_ID = @TUGROUPID)
	    ORDER BY a.TU_NAME


GO

GRANT EXEC ON dbo.PRC_TB_USER_SE TO Public

GO

